﻿using System;
using System.Collections.Generic;
using BuzzX8.Xmpp.StreamElements.Stanza;

namespace BuzzX8.Xmpp.StreamElements.Factories
{
    public class IqModel : StanzaModel<IqType>
    {
        public IqModel(PropertyDictionary parameters) : base(parameters)
        {
            ModelParameters["resourceBindInfo"] = new ResourceBindInfo(parameters);
            ModelParameters["rosterOperationData"] = new RosterOperationData(parameters);
        }

        public bool HasResourceBind
        {
            get { return ModelParameters.Get<bool>("hasResourceBind"); }
            set { ModelParameters["hasResourceBind"] = value; } 
        }

        public ResourceBindInfo ResourceBindInfo
        {
            get { return ModelParameters.Get<ResourceBindInfo>("resourceBindInfo"); }
        }

        public bool IsRosterOperation
        {
            get { return ModelParameters.Get<bool>("isRosterOperation"); }
            set { ModelParameters["isRosterOperation"] = value; }
        }

        public RosterOperationData RosterOperationData
        {
            get { return ModelParameters.Get<RosterOperationData>("rosterOperationData"); }
        }
    }

    public class ResourceBindInfo
    {
        private PropertyDictionary parameters;

        public ResourceBindInfo(PropertyDictionary parameters)
        {
            if(parameters == null)throw new ArgumentNullException("parameters");
            this.parameters = parameters;
        }

        public JID AssignedJid
        {
            get { return parameters.Get<JID>("resourceBindInfo.AssignedJid"); }
            set { parameters["resourceBindInfo.AssignedJid"] = value; }
        }

        public string RequestedResource
        {
            get { return parameters.Get<string>("resourceBindInfo.RequestedResource"); }
            set { parameters["resourceBindInfo.RequestedResource"] = value; }
        }
    }

    public class RosterOperationData
    {
        private PropertyDictionary parameters;

        public RosterOperationData(PropertyDictionary parameters)
        {
            if(parameters == null) throw new ArgumentNullException("parameters");
            this.parameters = parameters;
            this.parameters["rosterOperationData.ResponseItems"] = CreateRosterItemsCollection();
        }

        private ICollection<RosterItem> CreateRosterItemsCollection()
        {
            return new List<RosterItem>();
        }

        public RosterOperationType OperationType
        {
            get { return parameters.Get<RosterOperationType>("rosterOperationData.QueryType"); }
            set { parameters["rosterOperationData.QueryType"] = value; }
        }

        public RosterItem Item
        {
            get { return parameters.Get<RosterItem>("rosterOperationData.Item"); }
            set { parameters["rosterOperationData.Item"] = value; }
        }

        public ICollection<RosterItem> ResponseItems
        {
            get { return (ICollection<RosterItem>)parameters["rosterOperationData.ResponseItems"]; }
        }

        public void AddResponseItem(string itemName, JID jid)
        {
            ResponseItems.Add(new RosterItem(itemName, jid));
        }
    }

    public enum RosterOperationType
    {
        GetItems,
        ItemsResponse,
        AddItem,
        PushItem,
        UpdateItem,
        DeleteItem
    }
}
